package com.llk.rabbitmqlearn.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    /**
     *                  模拟tt，两个队列设置不同死信时间，到期后放入死信队列
     *                                      10s
     *                     /---------> normal_queue --------->\
     *              normal_routing_key                  dead_routing_key
     *                  /                                         \
     * start -> normal_exchange                                dead_queue -> dead_routing_key -> dead_exchange -> end
     *                  \                                         /
     *              normal_routing_key_2     40s        dead_routing_key
     *                    \---------> normal_queue_2 --------->/
     *              delay_routing_key                   dead_routing_key
     *                     \-------->   delayQueue   -------->/
     *
     */

    /**
     * 普通
     */
    public static final String NORMAL_EXCHANGE = "normal_exchange";
    public static final String NORMAL_QUEUE = "normal_queue";
    public static final String NORMAL_QUEUE_2 = "normal_queue_2";
    public static final String NORMAL_ROUTING_KEY = "normal_routing_key";
    public static final String NORMAL_ROUTING_KEY_2 = "normal_routing_key_2";

    /**
     * 死信
     */
    public static final String DEAD_EXCHANGE = "dead_exchange";
    public static final String DEAD_ROUTING_KEY = "dead_routing_key";
    public static final String DEAD_QUEUE = "dead_queue";

    /**
     * 延迟队列
     */
    public static final String DELAY_QUEUE = "delay_queue";
    public static final String DELAY_ROUTING_KEY = "delay_routing_key";

    /**
     * 高级发布确认
     */
    public static final String CONFIRM_EXCHANGE = "confirm_exchange";
    public static final String CONFIRM_QUEUE = "confirm_queue";
    public static final String CONFIRM_ROUTING_KEY = "confirm_routing_key";

    /**
     * 备份
     */
    public static final String BACKUP_EXCHANGE = "backup_exchange"; // fanout
    public static final String BACKUP_QUEUE = "backup_queue";
    public static final String WAINING_QUEUE = "waining_queue";



    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return new DirectExchange(NORMAL_EXCHANGE);
    }

    @Bean("normalQueue")
    public Queue normalQueue() {
        Map<String, Object> arguments = new HashMap<>(3);
        // 死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        // 死信路由键
        arguments.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
        // ttl 超时时间 ms
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(NORMAL_QUEUE).withArguments(arguments).build();
    }

    @Bean("normalQueue2")
    public Queue normalQueue2() {
        Map<String, Object> arguments = new HashMap<>(3);
        // 死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        // 死信路由键
        arguments.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
        // ttl 超时时间 ms
        arguments.put("x-message-ttl", 20000);
        return QueueBuilder.durable(NORMAL_QUEUE_2).withArguments(arguments).build();
    }

    @Bean("deadExchange")
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_EXCHANGE);
    }

    @Bean("deadQueue")
    public Queue deadQueue() {
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    @Bean
    public Binding normalQueueBindingNormalExchange(
            @Qualifier("normalQueue") Queue normalQueue,
            @Qualifier("normalExchange") DirectExchange normalExchange
    ) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with(NORMAL_ROUTING_KEY);
    }

    @Bean
    public Binding normalQueue2BindingNormalExchange(
            @Qualifier("normalQueue2") Queue normalQueue2,
            @Qualifier("normalExchange") DirectExchange normalExchange
    ) {
        return BindingBuilder.bind(normalQueue2).to(normalExchange).with(NORMAL_ROUTING_KEY_2);
    }

    @Bean
    public Binding deadQueueBindingDeadExchange(
            @Qualifier("deadQueue") Queue deadQueue,
            @Qualifier("deadExchange") DirectExchange deadExchange
    ) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY);
    }

    @Bean("delayQueue")
    public Queue delayQueue() {
        // 这次没有设置过期时间
        Map<String, Object> arguments = new HashMap<>(2);
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
        return QueueBuilder.durable(DELAY_QUEUE).withArguments(arguments).build();
    }

    @Bean
    public Binding delayQueueBindingNormalExchange(
            @Qualifier("delayQueue") Queue delayQueue,
            @Qualifier("normalExchange") DirectExchange normalExchange
    ) {
        return BindingBuilder.bind(delayQueue).to(normalExchange).with(DELAY_ROUTING_KEY);
    }

    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }

    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
//        return new DirectExchange(CONFIRM_EXCHANGE);
        // 修改创建方式，当消息到期未消费，由此交换机转发到备份交换机，启动项目前，需在控制台删除原来的交换器
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE).durable(true)
                .withArgument("alternate-exchange", BACKUP_EXCHANGE).build();
    }

    @Bean
    public Binding confirmQueueBindingConfirmExchange(
            @Qualifier("confirmQueue") Queue confirmQueue,
            @Qualifier("confirmExchange") DirectExchange confirmExchange
    ) {
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);
    }

    @Bean("backupExchange")
    public FanoutExchange backupExchange() {
        return new FanoutExchange(BACKUP_EXCHANGE);
    }

    @Bean("backupQueue")
    public Queue backupQueue() {
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }

    @Bean("waringQueue")
    public Queue waringQueue() {
        return QueueBuilder.durable(WAINING_QUEUE).build();
    }

    @Bean
    public Binding backupQueueBindingBackupExchange(
            @Qualifier("backupQueue") Queue backupQueue,
            @Qualifier("backupExchange") FanoutExchange backupExchange
    ) {
        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }

    @Bean
    public Binding waringQueueBindingBackupExchange(
            @Qualifier("waringQueue") Queue waringQueue,
            @Qualifier("backupExchange") FanoutExchange backupExchange
    ) {
        return BindingBuilder.bind(waringQueue).to(backupExchange);
    }
}
